Skip to content

fix: correct pinned maps handling and implement missing userspace setup#13

Open
SiyuanSun0736 wants to merge 1 commit intomultikernel:mainfrom
SiyuanSun0736:fix_pin_var_map
Open

fix: correct pinned maps handling and implement missing userspace setup#13
SiyuanSun0736 wants to merge 1 commit intomultikernel:mainfrom
SiyuanSun0736:fix_pin_var_map

Conversation

@SiyuanSun0736
Copy link
Contributor

Summary

This PR fixes critical issues in the pin keyword implementation for map declarations. Previously, pinned maps were incorrectly treated as global variables, leading to runtime failures. Additionally, this PR introduces a robust userspace setup including automatic directory creation and unique variable naming.

Problems Addressed

  • IR Generation Conflict: Pinned maps incorrectly generated both an IR map definition and an IR global variable. This caused userspace codegen to search for a non-existent __pinned_globals map, resulting in: Failed to find pinned globals map in eBPF object.
  • Missing Setup Logic: Map pinning logic was only triggered if the map was explicitly accessed in userspace C code. Pinned maps must be initialized even if used solely within the eBPF program.
  • Variable Name Collisions: Multiple pinned maps caused C compilation errors due to the hardcoded use of a single variable name (existing_fd) for all map instances.
  • FS Path Failures: The pinning process failed if the target directory in /sys/fs/bpf/ did not already exist.

Implementation Details (Before vs. After)

1. Fixing Incorrect Map Handling

Before: The generator erroneously looked for a __pinned_globals map for any pinned variable.

/* FAULTY LOGIC: Searching for non-existent global map */
pinned_globals_map_fd = bpf_obj_get("/sys/fs/bpf/maps_demo/globals/pinned_globals");
if (pinned_globals_map_fd < 0) {
    struct bpf_map *pinned_globals_map = bpf_object__find_map_by_name(obj->obj, "__pinned_globals");
    if (!pinned_globals_map) {
        fprintf(stderr, "Failed to find pinned globals map in eBPF object\n"); // <--- Runtime Error
        return 1;
    }
}

After: Directly handles the specific map and ensures the BPF file system path is ready.

/* CORRECTED LOGIC: Direct map handling with unique naming and directory setup */
struct bpf_map *event_log_map = bpf_object__find_map_by_name(obj->obj, "event_log");
int event_log_existing_fd = bpf_obj_get("/sys/fs/bpf/maps_demo/maps/event_log");

if (event_log_existing_fd >= 0) {
    event_log_fd = event_log_existing_fd;
} else {
    ensure_bpf_dir("/sys/fs/bpf/maps_demo/maps"); // Recursive directory creation
    bpf_map__pin(event_log_map, "/sys/fs/bpf/maps_demo/maps/event_log");
    event_log_fd = bpf_map__fd(event_log_map);
}

2. Robust Directory Creation

Added ensure_bpf_dir C helper function to recursively create paths (e.g., /sys/fs/bpf/<project>/maps/) before pinning, preventing "No such file or directory" errors.


Proposed Changes

  • IR Generator (ir_generator.ml): Modified to ensure map-type global declarations only produce map definitions, preventing redundant global variable entries.
  • Userspace Codegen (userspace_codegen.ml):
  • Forced map setup/FD declarations if has_pinned_maps is detected.
  • Implemented unique variable naming (e.g., %s_existing_fd) to support multiple pinned maps.
  • Injected ensure_bpf_dir helper and required headers (<sys/stat.h>, <sys/types.h>).

Verification Results

  • Test Case: examples/maps_demo.ks
  • Results: * Resolved the __pinned_globals lookup error.
  • Verified maps are correctly pinned to /sys/fs/bpf/maps_demo/maps/.
  • Verified successful compilation and multi-map support.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant